<template>
  <view class="wrapper">
    <SearchBar @search="toSearchList"/>
    <view class="history-search">
      <view class="title">
        <text class="title">历史搜索</text>
        <icon type="clear"
              size="18" @click="clearHistory">
        </icon>
      </view>
      <view >
				<view v-for="(item, index) in historyList" @click="toSearchList(item)" >
					{{item}}
				</view>
       <!-- <view>小米</view>
        <view>华为手机</view>
        <view>锤子手机</view>
        <view>iPad</view> -->
      </view>
    </view>
  </view>
</template>

<script>
import SearchBar from '../../components/searchBar.vue'
const HISTORY_LIST = 'history_list'
export default {
  components: {
    SearchBar
  },
	data() {
		return {
			// 存储关键子
			historyList:[]
		}
	},
	onShow(){
		// 页面初始化时，当搜索页面从其他页面返回时,把storage=>data.属性
		this.historyList = uni.getStorageSync(HISTORY_LIST)||[]
	},
	methods:{
		// 清除弹框
		clearHistory () {
			uni.showModal({
				title:'提示',
				content:'确定要删除历史搜索吗集美/铁汁？',
				success: (res) => {
					// 用户确认
					if (res.confirm){
						// 删除页面缓存
						uni.removeStorageSync(HISTORY_LIST)
						// 清空历史搜索列表
						this.historyList=[]
					}
				}
			})
		},
		// 子传父(跳转到搜索列表将关键字传递过去)
		toSearchList (keyWord){
			uni.navigateTo({
				url:'/pages/search_list/search_list?catName='+ keyWord
			})
			// 插入头部
			let historyData=[...new Set([keyWord, ...this.historyList])]
			// 本地保存关键字
			uni.setStorageSync(HISTORY_LIST, historyData)
		}
	}
}
</script>

<style lang="less">
.search {
  background-color: #eee;
  padding: 30rpx 15rpx;
  display: flex;
  justify-content: space-between;
  font-size: 28rpx;
  position: relative;
  icon {
    position: absolute;
    top: 50rpx;
    left: 38rpx;
  }
  input {
    height: 60rpx;
    flex: 1;
    background-color: #fff;
    padding-left: 70rpx;
    box-sizing: border-box;
    border-radius: 4rpx;
  }
  button {
    width: 160rpx;
    height: 60rpx;
    line-height: 60rpx;
    border-radius: 8rpx;
    font-size: 28rpx;
    border: 1rpx solid #bfbfbf;
    background-color: #eee;
    margin-left: 20rpx;
  }
}

.history-search {
  color: #333;
  font-size: 28rpx;
  padding: 30rpx 30rpx 30rpx 15rpx;
  .title {
    display: flex;
    justify-content: space-between;
  }

  view {
    display: flex;
    flex-wrap: wrap;
    margin-top: 30rpx;
    view {
      height: 64rpx;
      line-height: 64rpx;
      padding: 0 26rpx;
      background-color: #ddd;
      margin: 0 30rpx 16rpx 0;
			
    }
  }
}
</style>
